package com.example.prison.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.CarManagement;
import com.example.prison.entity.PeopleManage;
import com.example.prison.service.CarManagementService;
import com.example.prison.service.SystemLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "车辆管理")
@RestController
@RequestMapping("/api")
public class CarManagementController {

    @Autowired
    private CarManagementService carManagementService;
    @Autowired
    private SystemLogService systemLogService;

    @ApiOperation("获取车辆列表")
    @GetMapping("/getCar")
    public Response<PageInfo<CarManagement>> getCarManagement(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                              @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                              @Parameter(description = "车牌号") @RequestParam(required = false) String carNumber,
                                                              @Parameter(description = "部门") @RequestParam(required = false) String carDepart,
                                                              @Parameter(description = "车辆状态(营区内/营区外)") @RequestParam(required = false) String carStatus) {
        PageInfo<CarManagement> pageInfo = carManagementService.get(pageNumber, pageSize, carNumber, carDepart, carStatus);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("新增车辆")
    @PostMapping("/addCar")
    public Response<?> addCarManagement(@RequestBody CarManagement car) {
        car.setCarStatus("营区内");
        int result = carManagementService.add(car);
        if (result == -1) {
            return Response.error("车辆已存在！");
        } else if (result == 0) {
            return Response.error("无此权限！");
        }
        log.info("新增车辆" + ", 数据：" + JSON.toJSONString(car));
        systemLogService.add("新增车辆");
        return Response.success("添加成功！");
    }

    @ApiOperation("删除车辆")
    @PostMapping("/deleteCar")
    public Response<?> deleteCar(@Parameter(description = "车辆ID") @RequestBody Integer[] ids) {
        carManagementService.delete(ids);
        log.info("删除车辆");
        systemLogService.add("删除车辆");
        return Response.success("删除成功！");
    }

    @ApiOperation("编辑车辆")
    @PostMapping("/updateCar")
    public Response<?> updateCar(@RequestBody CarManagement car) {
        Integer id = car.getCarId();
        int result = carManagementService.update(car, id);
        if (result == -1) {
            return Response.error("车辆已存在！");
        } else if (result == 0) {
            return Response.error("车辆外出中，编辑失败！请稍后编辑");
        }
        log.info("编辑车辆" + ", 数据：" + JSON.toJSONString(car));
        systemLogService.add("编辑车辆");
        return Response.success("更新成功！");
    }

    @ApiOperation("依据id获取车辆")
    @GetMapping("/getCarById")
    public Response<CarManagement> getCarById(@Parameter(description = "车辆ID") @RequestParam(value = "carId") Integer carId) {
        CarManagement car = carManagementService.getById(carId);
        if (ObjectUtils.isEmpty(car)) {
            return Response.error("此车辆id不存在！");
        }
        return Response.success("获取成功！", car);
    }

    @ApiOperation("获取所有内部车辆")
    @GetMapping("/getInsideCar")
    public Response<PageInfo<CarManagement>> getInsideCar(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        List<CarManagement> data = carManagementService.getInsideCar();
        PageInfo<CarManagement> pageInfo = new PageInfo<>(data);
        if(ObjectUtils.isEmpty(data)) {
            return Response.error("此人员id不存在！");
        }
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("获取所有外部车辆")
    @GetMapping("/getOutsideCar")
    public Response<PageInfo<CarManagement>> getOutsideCar(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                          @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        List<CarManagement> data = carManagementService.getOutsideCar();
        PageInfo<CarManagement> pageInfo = new PageInfo<>(data);
        if(ObjectUtils.isEmpty(data)) {
            return Response.error("此人员id不存在！");
        }
        return Response.success("获取成功！", pageInfo);
    }
}

